import os
import matplotlib.pyplot as plt
import IPython.display as ipd

import json
import math
import torch
from torch import nn
from torch.nn import functional as F
from torch.utils.data import DataLoader

import commons
import utils
from data_utils import TextAudioLoader, TextAudioCollate, TextAudioSpeakerLoader, TextAudioSpeakerCollate
from models import SynthesizerTrn
from text.symbols import symbols
from text import text_to_sequence

from scipy.io.wavfile import write
import soundfile as sf
import simpleaudio as sa


def get_text(text, hps):
    text_norm = text_to_sequence(text, hps.data.text_cleaners)
    if hps.data.add_blank:
        text_norm = commons.intersperse(text_norm, 0)
    text_norm = torch.LongTensor(text_norm)
    return text_norm


def text2speak(text):
    hps = utils.get_hparams_from_file("./configs/biaobei_base.json")

    net_g = SynthesizerTrn(
        len(symbols),
        hps.data.filter_length // 2 + 1,
        hps.train.segment_size // hps.data.hop_length,
        **hps.model).cuda()
    _ = net_g.eval()

    _ = utils.load_checkpoint('./G_1434000.pth', net_g, None)

    text = text
    length_scale = 1  # @param {type:"slider", min:0.1, max:3, step:0.05}
    filename = 'test'  # @param {type: "string"}
    audio_path = f'./{filename}.wav'
    stn_tst = get_text(text, hps)
    with torch.no_grad():
        x_tst = stn_tst.cuda().unsqueeze(0)
        x_tst_lengths = torch.LongTensor([stn_tst.size(0)]).cuda()
        audio = net_g.infer(x_tst, x_tst_lengths, noise_scale=.667, noise_scale_w=0.8, length_scale=length_scale)[0][
            0, 0].data.cpu().float().numpy()
    # ipd.display(ipd.Audio(audio, rate=hps.data.sampling_rate))
    sf.write(audio_path, audio, samplerate=hps.data.sampling_rate)
    sa.WaveObject.from_wave_file("test.wav").play().wait_done()


if __name__ == "__main__":
    text2speak("你好，我叫派蒙，很高兴认识你！")
